var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cors = require('cors');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users_login');  // 用户登录
var userRouter = require('./routes/users');  // 用户增删改查
var newsRouter = require('./routes/news');  // 用户增删改查
var taskRouter = require('./routes/tesk');  // 任务增删改查
var dayPlanRouter = require('./routes/dayPlan'); //日计划增删改查

var app = express();
app.use(cors())
app.use("/upload",express.static('upload'));//图片上传地址
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// app.use('/images/imgs', express.static('images/imgs'));  //图片上传地址

app.use('/', indexRouter); // 不使用
app.use('/users_login', usersRouter); //用户登录路由
app.use('/users', userRouter); //用户登录路由
app.use('/news', newsRouter); //新闻管理路由
app.use('/task', taskRouter); //任务管理路由
app.use('/dayPlan',dayPlanRouter);//日计划路由



// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404));
});

// error handler
app.use(function (err, req, res) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;
